import {_decorator, Component, Node} from 'cc';
import {Long} from "protobufjs";
import {PlayerSideView} from "db://assets/script/game/majiang/view/playerSideView";
import {pb} from "db://assets/script/network/ws/proto";
import {pt} from "db://assets/script/game/player/api";

const { ccclass, property } = _decorator;

@ccclass('PlayerView')
export class PlayerView extends Component {

    @property({
        type: Node,
        tooltip: "本家"
    })
    mySide: Node

    @property({
        type: Node,
        tooltip: "上家"
    })
    leftSide: Node

    @property({
        type: Node,
        tooltip: "对家"
    })
    topSide: Node

    @property({
        type: Node,
        tooltip: "下家"
    })
    rightSide: Node

    sideOrder: Node[]
    // 玩家对应的side
    playerSideNode: {}

    onLoad() {
        // TODO 根据人数初始化 sideOrder
        this.sideOrder = [this.mySide, this.leftSide, this.topSide, this.rightSide]
        this.mySide.active = false
        this.leftSide.active = false
        this.topSide.active = false
        this.rightSide.active = false
        this.playerSideNode = {}
    }

    // 初始化玩家信息
    initPlayerInfo(index: number, avatar: string, nickname: string, ruby: number, shortId: number, isReady: boolean) {
         const node = this.sideOrder[index]
        if (!node) {
            return
        }
        if (!node.active) {
            // 显示
            node.active = true
        }
        const view = node.getComponent(PlayerSideView)
        view.updateAvatar(avatar, nickname, ruby)
        view.toggleReadyHand(isReady)
        this.playerSideNode[shortId] = node
    }

    // 展示，隐藏玩家
    togglePlayerByIndex(index: number, isShow: boolean) {
        const view = this.getPlayerSideViewByIndex(index)
        if (view) {
            view.togglePlayer(isShow)
        }
    }

    toggleReadyHandByIndex(index: number, isShow: boolean) {
        const view = this.getPlayerSideViewByIndex(index)
        if (view) {
            view.toggleReadyHand(isShow)
        }
    }

    toggleReadyHandByShortId(shortId: number | Long, isShow: boolean) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            view.toggleReadyHand(isShow)
        }
    }

    // 显示牌
    addCardByShortId(shortId: number, list: pb.ITileInfo[]) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            for (const info of list) {
                view.addCard(info, true)
            }
        }
    }

    // 添加摸牌
    addNewTile(shortId: number, tile: pb.ITileInfo) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            // 添加一张空白牌
            const blankCard: pb.ITileInfo = {
                tid: pt.TileId.noCard,
                index: -1,
            }
            view.addCard(blankCard, false)
            // 再加上新摸的
            view.addCard(tile, true)
        }
    }

    // 显示牌
    updateCardPositionByShortId(shortId: number) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            view.updateCardPosition()
        }
    }

    // 显示牌
    listenPlayerCardByShortId(shortId: number) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            view.listenPlayerCard()
        }
    }

    // 用户退出
    playerViewExit(shortId: number | Long) {
        const node = this.playerSideNode[Number(shortId)]
        if (node) {
            node.active = false
        }
        delete this.playerSideNode[Number(shortId)]
    }

    resetCardsByShortId(shortId: number) {
        const view = this.getPlayerSideViewByShortId(shortId)
        if (view) {
            view.resetCards()
        }
    }

    getPlayerSideViewByShortId(shortId: number | Long) {
        const node = this.playerSideNode[Number(shortId)]
        if (node) {
            return node.getComponent(PlayerSideView)
        }
        return null
    }

    getPlayerSideViewByIndex(index: number) {
        const node = this.sideOrder[index]
        if (node) {
            return node.getComponent(PlayerSideView)
        }
        return null
    }

    // // 玩家掉线
    // playerOffline(shortId: number) {
    //     // TODO 添加离线标志
    // }
}


